#include "TileId.h"
#include <assert.h>
namespace VIS
{
	TileId::TileId():m_mapThemeIdHash(0), m_zoomLevel(0), m_tileX(0), m_tileY(0)
	{
	}

	TileId::TileId(string const& _mapThemeId, int _zoomLevel, int _tileX, int _tileY)
		:m_zoomLevel(_zoomLevel), m_tileX(_tileX), m_tileY(_tileY)
	{
		std::hash<string> str_hash;
		int hash = str_hash(_mapThemeId);
		m_mapThemeIdHash = hash;
	}

	TileId::TileId(unsigned int _mapThemeIdHash, int _zoomLevel, int _tileX, int _tileY)
		:m_mapThemeIdHash(_mapThemeIdHash), m_zoomLevel(_zoomLevel), m_tileX(_tileX), m_tileY(_tileY)
	{
	}

	TileId::~TileId(void)
	{
	}

	TileId TileId::fromString(string const& idStr)
	{
		string components[4];
		size_t found = idStr.find_first_of(':');
		assert(found != string::npos);
		components[0] = idStr.substr(0,found);
		size_t found2 = idStr.find_first_of(':',found+1);
		assert(found2 != string::npos);
		components[1] = idStr.substr(found+1,found2-2);
		found = found2;
		found2 = idStr.find_first_of(':',found+1);
		assert(found2 != string::npos);
		components[2] = idStr.substr(found+1,found2-2);
		components[3] = idStr.substr(found2, idStr.length()-1-found2);

		unsigned int const mapThemeIdHash = atoi(components[ 0 ].c_str());
		int const zoomLevel = atoi(components[ 1 ].c_str());
		int const tileX = atoi(components[ 2 ].c_str());
		int const tileY = atoi(components[ 3 ].c_str());
		return TileId( mapThemeIdHash, zoomLevel, tileX, tileY );
	}

}
